<template>
    <div>
        <h3>增加列表</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>科室名称</td>
                    <td>
                        <input type="text" v-model="info.deskName">
                    </td>
                </tr>
                <tr>
                    <td>科室图片</td>
                    <td>
                        <input type="file" @change="Imgs">
                        <img :src="info.deskImg" v-if="info.deskImg != ''">
                    </td>
                </tr>
                <tr>
                    <td>成立日期</td>
                    <td>
                        <input type="date" v-model="info.deskTime">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" class="btn btn-success" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router';
const router = useRouter()
const route = useRoute()
//接收数据
let info = reactive({
    "deskName": "",
    "deskImg": "",
    "deskTime": ""
})
const add = () => {
    axios({
        url: '/api/Desk/AddDesk',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert('科室名称重复')
                return
            }
            if (res.data > 0) {
                alert('添加成功')
            }
            else {
                alert('添加失败')
                return
            }
        })
        .catch((err) => {
            console.log(err);

        })
}
const Imgs = (e: any) => {
    let obj = e.target.files[0]
    var f = new FormData()
    f.append("file", obj)
    axios({
        url: '/api/Desk/Imgs',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            if (res.data == "不是图片") {
                alert('不是图片')
                return
            }
            if (res.data == "大于2M") {
                alert('大于2M')
                return
            }
            info.deskImg = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}
</script>

<style scoped></style>